import Vue from 'vue'
import Router from 'vue-router'
import Layout from '../views/layout/Layout' // 主容器组件
import { isLogin } from '@/api/login'
const _import = require('./_import_' + process.env.NODE_ENV + '.js') // 加载路由方法（这样就不需要申明了）

Vue.use(Router)

export const constantRouterMap = [ // 公共路由
  { path: '', redirect: '/login' },
  { path: '/login', component: _import('login/login'), hidden: true }, // huidden 用来表示是否显示在左侧主导航上
  { path: '',
    component: Layout,
    children: [{
      path: 'index',
      component: _import('index/index'),
      name: 'index',
      icon: 'index',
      title: '主页管理'
    }]
  },
  { path: '',
    component: Layout,
    children: [{
      path: 'dashboard',
      component: _import('dashboard/dashboard'),
      name: 'dashboard',
      icon: 'dashboard',
      title: '导航管理'
    }]
  },
  { path: '',
    component: Layout,
    hidden: true,
    children: [{
      path: 'editNav',
      component: _import('dashboard/editNav'),
      name: 'editNav',
      title: '导航完善'
    }]
  },
  {
    path: '',
    component: Layout,
    redirect: 'noredirect',
    children: [{
      path: 'articleClassify',
      component: _import('article/arricle-classification'),
      name: 'articleClassify',
      icon: 'cla',
      title: '课程分类管理'
    }]
  },
  {
    path: '/course',
    component: Layout,
    redirect: 'noredirect',
    meta: {
      title: '课程管理',
      icon: 'article'
    },
    children: [
      {path: 'articleAdd', component: _import('article/article-form'), name: 'articleAdd', icon: 'add', title: '课程添加'}
      // { path: 'articleList', component: _import('article/article-list'), name: 'articleList', meta: {name: 'articleList', title: '课程列表', icon: 'list'} }
    ]
  },
  {
    path: '',
    component: Layout,
    redirect: 'noredirect',
    children: [{
      path: 'address',
      component: _import('address/address'),
      name: 'address',
      icon: 'address',
      title: '上课地址管理'
    }]
  },
  {
    path: '',
    component: Layout,
    redirect: 'noredirect',
    children: [{
      path: 'order',
      component: _import('order/order'),
      name: 'order',
      icon: 'order',
      title: '订单管理'
    }]
  },
  {
    path: '',
    component: Layout,
    redirect: 'noredirect',
    children: [{
      path: 'invoice',
      component: _import('invoice/invoice'),
      name: 'invoice',
      icon: 'invoice',
      title: '发票管理'
    }]
  },
  {
    path: '',
    component: Layout,
    redirect: 'noredirect',
    meta: {
      title: '个人设置',
      icon: 'personal'
    },
    children: [{
      path: 'personal',
      component: _import('personal/personal'),
      name: 'personal',
      icon: 'personal',
      meta: {name: 'personal', title: '支付配置', icon: 'pay'}
    }, {
      path: 'communicate',
      component: _import('personal/communicate'),
      name: 'communicate',
      meta: {name: 'communicate', title: '联系我们', icon: 'usered'}
    }]
  },
  {
    path: '',
    component: Layout,
    redirect: 'noredirect',
    children: [{
      path: 'user',
      component: _import('user/user'),
      name: 'user',
      icon: 'usered',
      title: '个人中心'
    }]
  },
  {
    path: '*',
    component: Layout,
    redirect: 'login'
  }
]

let myRouter = new Router({
  mode: 'history',
  routes: constantRouterMap
})
// 判断是否登录
myRouter.beforeEach((to, from, next) => {
  isLogin().then(res => {
    if (res.code === 1) {
      next()
    } else {
      next('/login')
    }
  })
})

export default myRouter
